#include "Model3DUtils.h"

#include "Ground.h"
#include "SharedConstants.h"

namespace Model3DUtils
  {
  bool CanPlaceObjectOn(const std::pair<size_t, size_t>& i_cell, const Ground* ip_ground, double i_max_height_diff)
    {
    TVector3 points[4] = {TVector3(i_cell.first * GroundConstants::CELL_SIZE, i_cell.second * GroundConstants::CELL_SIZE, 0),
                          TVector3((i_cell.first + 1) * GroundConstants::CELL_SIZE, i_cell.second * GroundConstants::CELL_SIZE, 0),
                          TVector3((i_cell.first + 1) * GroundConstants::CELL_SIZE, (i_cell.second + 1) * GroundConstants::CELL_SIZE, 0),
                          TVector3(i_cell.first * GroundConstants::CELL_SIZE, (i_cell.second + 1) * GroundConstants::CELL_SIZE, 0)};
    for (size_t i = 0; i < 4; ++i)
      points[i][2] = ip_ground->GetHeightForXY(TVector2(points[i][0], points[i][1])) * GroundConstants::CELL_SIZE;
  
    double min_h = points[0].Z(), max_h = min_h; 
    for (size_t i = 0; i < 4; ++i)
      {
      min_h = std::min(min_h, points[i].Z());
      max_h = std::max(max_h, points[i].Z());
      }
    if (max_h - min_h > i_max_height_diff)
      {
      return false;
      }
    return true;
    }
  }
